// 异步数据处理

function after(timers, callback) {
  let res = {};
  return function (key, val) {
    res[key] = val;
    if (--timers === 0) {
      callback(res);
    }
  };
}

let newFn = after(2, function (data) {
  console.log(data);
});

const fs = require('fs');

fs.readFile('name.txt', 'utf8', function (err, data) {
  newFn('name', data);
  console.log(data);
});

fs.readFile('age.txt', 'utf8', function (err, data) {
  newFn('age', data);
  console.log(data);
});




Promise.resolve().then(() => {
  console.log("1");
  Promise.resolve().then(() => {
    console.log("2");
  }).then((() => {
    console.log("3");
  }).then(() => {
    console.log("4");
  })
}).then(() => {
  console.log("5");
}).then(() => {
  console.log("6");
})






















async function asyn1 () {
  console.log("async1 start");
  await asyn2();
  console.log('asyn1 end');
}

async function asyn2 () {
  console.log("async2 start");
}

setTimeout(function () {
  console.log("timerout");
}, 0);

console.log("script start");
asyn1();

new Promise(function (resolve, reject) {
  console.log("promise1");
  resolve();
}).then(res => {
  console.log("resolve");
});
console.log("script end");

// script start, async1 start, async2 start, promise1, script end, asyn1 end  ,  resolve  ,timerout